/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.widgets.Canvas;

/**
 *
 * @author Oren
 */
public class SmartRobot implements Robot {
    Robot robot;
    int current;
    int smartspeed ;
    String Type = "Smart Robot of";
    private boolean assign = false;
    int thetarget;

    public SmartRobot(Robot rbt)
    {
        this.robot = rbt;
        current = 0 ;
        smartspeed =0 ;
        assign = false;
        robot.setType(this.Type);

    }

    public void setStartingPosition(Position p) {
        robot.setStartingPosition(p);
        current = 0 ;
        smartspeed =0 ;
    }
    public int mod (int num, int moder)
    {
        if (num>0)
            return num%moder;
        num = num%moder;
        if (num!=0)
            return num+moder;
        else
            return 0;
    }

    public synchronized void move(int speed, int heading) {

        int left_target = 0 , right_target = 0 ;
        int hnew = mod(heading,360) ;
        // the move function happens only once move is called from the program
        // and only one time. from now on it is the act method to update
        // the robot heading.

        // get the robot heading
        current = robot.getHeading();
        if ((hnew - current) >= 0)
        {
            right_target = hnew - current;
            left_target = 360 - right_target;
        }
        if ((hnew - current) < 0)
        {
            left_target = current - hnew  ;
            right_target = 360 - (left_target) ;
        }
        // positive values are for right direction movement
        if (right_target<=180)
            thetarget = hnew;
                    
        //negative values are for left direction movement
        else
            thetarget = hnew - 360 ;
        smartspeed = speed;
        robot.move(smartspeed, thetarget);
        System.out.println("t"+thetarget);
        System.out.println("h"+hnew);
        System.out.println("c"+current);


    }
    public void update(){
        robot.move(smartspeed, thetarget);


    }


    public void stop() {
        smartspeed = 0;
        robot.stop();
    }

    public Position getCurrentPosition(){
        return robot.getCurrentPosition();
    }

    public void act() {
        robot.act();
        update();
    }

    public String getName() {
        return robot.getName();
    }

    public void setName(String name) {
        robot.setName(name);
    }

    public int getDistance() {
        return robot.getDistance();
    }

    public int compareTo(Robot arg0) {
        return robot.compareTo(arg0);
    }

    public void setPre(Position temp) {
        robot.setPre(temp);
    }

    public Position getStartPosition() {
        return robot.getStartPosition();
    }

    public int getSpeedLimit() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getHeadingLimit() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getHeading() {
        return robot.getHeading();
    }

    public boolean pickupBox(Box b) {
        return false;
    }

    public boolean putdownBox() {
         return false;
    }

    public String getType()
    {
       return Type;

    }
    public void setAssign()
    {
        assign = true;
    }
    public boolean getAssign()
  {
      return assign;
  }
    public void paint(PaintEvent e){
        
        robot.paint(e);
        
}


    public void setType(String type) {
        
        robot.setType(type);
    }
}

